package com.foreveross.weixin.util;

import java.beans.PropertyDescriptor;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.foreveross.weixin.common.Constant;
import com.foreveross.weixin.vo.request.BaseMessage;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * 字符串封装类
 * @author wm
 * @date 2014-3-11
 */
public class StringUtil {
	
	/**
	 * 将对象型的字符串转换为相应的对象
	 * @param str
	 * @param clazz
	 * @return
	 */
	public static <T extends Object> T StrToClazz(String str, Class<T> clazz){
		Gson gson = new Gson();
        T t = gson.fromJson(str, clazz);
		return t;
	}
	
	
	/**
	 * 将对象型的字符串转换为相应的对象
	 * @param str
	 * @param clazz
	 * @return
	 */
	public static String  ClazzToJsonStr(Object obj){
		GsonBuilder gb = new GsonBuilder();
		gb.disableHtmlEscaping();
        String json = gb.create().toJson(obj);
		return json;
	}
	
	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 */
	public static boolean isNullOrEmpty(String str)
	{
		if (str==null || str.length()==0)
		return true;
		else return false;
	}
	
	
	public static <T extends Object> T MapToClazz(Map map,Class<T> clazz){
		T t = null;
		try{
			t = clazz.newInstance();
			Field[] fileds = clazz.getDeclaredFields();
			
			 //写数据
			for(Field field : fileds) {
				PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
				//获得写方法
				Method method = pd.getWriteMethod();
				Class fieldType = field.getType();
				String value = (String)map.get(field.getName());
				if(value != null && !"".equals(value) && fieldType.equals(int.class)){
					method.invoke(t, Integer.parseInt(value));
				}else if(value != null && !"".equals(value) && fieldType.equals(long.class)){
					method.invoke(t, Long.parseLong(value));
				}else if(value != null && !"".equals(value) && fieldType.equals(float.class)){
					method.invoke(t, Float.parseFloat(value));
				}else if(value != null && !"".equals(value) && fieldType.equals(Boolean.class)){
					method.invoke(t, Boolean.parseBoolean(value));
				}else if(value != null && !"".equals(value) && fieldType.equals(double.class)){
					method.invoke(t, Double.parseDouble(value));
				}else if(value != null && !"".equals(value) && fieldType.equals(String.class)){
					method.invoke(t, (String)map.get(field.getName()));
				}
				
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return t;
	}
	
	public static String getTradeNo(){
		DateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
		String dateStr = sdf.format(new Date());
		return "HRNO" + dateStr;
	}
	
    /**
     * 生成nonce
     * @return
     */
    public static String create_nonce_str() {
        return UUID.randomUUID().toString().substring(0, 32);
    }

    /**
     * 生成timestamp
     * @return
     */
    public static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }
	
	public static void main(String[] args) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("ToUserName", "gh_9629927a275d");
		map.put("FromUserName", "oEJ6GwMPDtyLF6Ohlgy8gJdTMZFg");
		map.put("CreateTime", "1441548193");
		map.put("EventKey", "http:\\kdkc365.com/WeChatForeEnd/whatsnew/index.html#newsCenter/newsCenter-index.html?newsType=0");
		map.put("Event", "VIEW");
		map.put("Encrypt", "3ke77En2vM83aTe3CJYW/g/hDueuulESAQ+HVQDBD319woBN/wAlZFe5pSR0HGSUVHQVmdOqU4d0rrtxXB6NsLWR4HH3jZntZ77YA1zFmSzyNWLkAv6AbeZj6MhaCKxfxHQPlFDcu+BqeuVvsfpOeZVDssbMOOt+hUOh1yoPWF9EodclH6BBLNOH/Z0puf+CWW/QXzvG+myd7+ilI2927/zEj+RztBe/GdizKn5WW4bjO2DHlmpvmD7CT44Su/GbazefZON6kiU9kJTlZDzutD/AjGOmXwgKrj4JEFmw8/Awwh54YuAVhWCyVGUfb3PaMGDoVDjDt3XcyDs1BABIfNaqsCXG+W2bgi3r1M6R58dy6rNPvj+JsGCWgEHKMfJhoAHbHbstDacVDYXku3akQosOiZWkB3EQjkitngpOyIrsVhtl78jPEPVBq6zS5WxoB601KCqSUzFQcBTdEdNgOhcRUM904YT0ew2AfQEE5IeeX5TOeV5TjN5tLWBKCDC4moA5hZMZ+3suoZ4ckSsxC1pBVtBl5gqTxXQq9+zsxkg=, ");
		map.put("MsgType", "event");
		
		BaseMessage baseMessage = MapToClazz(map, BaseMessage.class);
		System.out.println(baseMessage.getToUserName());
	}
}
